﻿Imports System.Data
Imports System
Imports System.IO
Partial Class Content_Reportes_rep_controls_ReporteDetalleCartera
    Inherits System.Web.UI.UserControl
    Dim ECConnString As String = ConfigurationManager.AppSettings("dbIP")
    Dim IControles As New Clases.controles
    Dim IDataBase As New Clases.DataBase

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim q1, q2 As String

            q1 = "Select * from TDespachos where stat_id=1"
            IControles.fill_dropDownListBaseQuery(ddlDespacho, "TDespachos", "desp_id", "desp_nombre", q1)
            IControles.addNoneAllElement(ddlDespacho)

            q2 = "Select * from TInstituciones where stat_id=1"
            IControles.fill_dropDownListBaseQuery(ddlInstitucion, "TInstituciones", "inst_id", "inst_nombre", q2)
            IControles.addNoneAllElement(ddlInstitucion)

            fecha.Attributes.Add("onKeyPress", "return false;")
            fecha.Attributes.Add("onPaste", "return false;")
            'hl_pdf.Visible = False
        End If
    End Sub

    Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
        'hl_pdf.Visible = True

        Dim qryBusq, id, eid, capital, ecatpital, valor, eval As String
        'qryBusq = "Select desp_nombre,inst_nombre,per_nombre,cl_nombre,mov_numero_titulos,mov_costo,ValMercado,cl_cartera,cl_contrato,per_capitales,per_tipo_a,per_tipo_b,per_tipo_c,per_notas_estruc,per_deuda,per_fondo_inv,dm_fecha_registro from vwReporteDetalleCartera where cl_nombre like '" & bCliente.Text & "%' "
        'id = "Select cl_id from vwDetalleMovT2 where Nombre like '" & bCliente.Text & "%' "
        'id = "Select cl_id from vwReporteDetalleCartera where Nombre like '" & bCliente.Text & "%' "
        id = "Select cl_id from TClientes where cl_nombre like '" & bCliente.Text & "%' "
        eid = IDataBase.getSimpleData(id, "cl_id")
        
        'emisoraid = "Select emi_id from vwReporteDetalleCartera where Nombre like '" & bCliente.Text & "%' "
        'eemisoraid = IDataBase.getSimpleData(emisoraid, "emi_id")
        'cont = "select cl_contrato from TClientes where cl_id = " & eid
        'econt = IDataBase.getSimpleData(cont, "cl_contrato")
        'bContrato.Text = econt
        'bContrato.Attributes("style") = "TEXT-ALIGN: center"
        'txtValor.text = (Format(Val(Me.txtCantidad.Text), "#,##0.00"))
        'bContrato.Text = (Format(Val(Me.bContrato.Text), "#,##0.00"))


        'If bContrato.Text <> "" Then
        '    qryBusq = qryBusq & " and cl_contrato like '" & bContrato.Text & "%'"
        'End If

        'qryBusq = "Select * from vwDetalleMovT2 where Nombre like '" & bCliente.Text & "%' "
        'qryBusq = "select v.Emisora, v.tipo_nombre, e.emi_serie, v.[Fecha de Operación], v.[Fecha de Liquidación], v.mov_operacion, v.mov_tipo, v.mov_activo, v.Despacho, v.Institución, v.Titulos, v.mov_costo, v.mov_plazo, v.mov_tasa, v.mov_plazo_cupon from vwDetalleMovT2 v, tEmisoras e where e.emi_id = v.emi_id and  Nombre like '" & bCliente.Text & "%' "
        'qryBusq = "select Emisora, tipo_nombre, emi_serie, mov_fecha_op, mov_fecha_liquidez, mov_operacion, mov_activo, mov_tipo, Despacho, Institución, num_titulos, mov_costo, mov_tasa, mov_plazo, mov_plazo_cupon from vwReporteDetalleCartera  where Nombre like '%" & bCliente.Text & "%' "
        qryBusq = "select Emisora, Tipo, Serie, FechaOperacion, FechaLiquidez, Operacion, Activo, TipoM, Despacho, Institución, Titulos, Costo, Tasa, Plazo, PlazoCupon from vwReporteDetalleCartera  where Nombre like '%" & bCliente.Text & "%' "
        If ddlDespacho.SelectedValue <> "None" Then
            qryBusq = qryBusq & " and desp_id=" & ddlDespacho.SelectedValue & ""
        End If

        If ddlInstitucion.SelectedValue <> "None" Then
            qryBusq = qryBusq & " and inst_id=" & ddlInstitucion.SelectedValue & ""
        End If

        If fecha.Text <> "" Then
            qryBusq = qryBusq & " and FechaOperacion <= '" & fecha.Text & "'"
        End If

        Session("RepMovi") = qryBusq
        hf_repDC.Value = qryBusq
        IControles.fillGridViewPaging(gvDetalleCartera, qryBusq)
        'select  (cl_cartera+(dbo.fnTitulos(4,1)*dbo.fnCosto(4,1))) as total from TClientes where cl_id=4 
        'val = "select  (cl_cartera+(dbo.fnTitulos(" & eid & "," & eemisoraid & ")*dbo.fnCosto(" & eemisoraid & "))) as total from TClientes where cl_id=" & eid
        'val = "select dbo.valorCarteracv(" & eid & ")"
        valor = "select dbo.valorCarteracv(" & eid & ") as total"
        eval = IDataBase.getSimpleData(valor, "total")        
        txtCartera.Text = eval
        'txtCartera.Text = (Format(Val(eval), "$###,###,###.#######################"))
        'Label1.Attributes("style") = "TEXT-ALIGN: right"
        txtCartera.Attributes("style") = "TEXT-ALIGN: center"
        'hl_pdf.Visible = True
        btn_excel.Visible = True

    End Sub

    Protected Sub exportToExcel(ByVal query As String)
        'For i As Integer = 0 To gridView_toExport.Rows.Count - 1
        '    Dim row As GridViewRow = gridView_toExport.Rows(i)
        '    row.Cells(0).Visible = False
        'Next

        'Dim sb As StringBuilder = New StringBuilder()
        'Dim sw As IO.StringWriter = New IO.StringWriter(sb)
        'Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
        'Dim pagina As Page = New Page
        'Dim form = New HtmlForm
        'pagina.EnableEventValidation = False
        ''pagina.EnableEventValidation = True
        'pagina.DesignerInitialize()
        'pagina.Controls.Add(form)
        'gridView_toExport.AllowPaging = False
        'form.Controls.Add(gridView_toExport)
        'pagina.RenderControl(htw)
        'Response.Clear()
        'Response.Buffer = True
        'Response.ContentType = "application/vnd.ms-excel"
        ''Response.ContentType = "application/excel"
        'Response.AddHeader("Content-Disposition", "attachment;filename=ReporteDetalleCartera" & ".xls")
        'Response.Charset = "UTF-8"
        'Response.ContentEncoding = Encoding.Default
        'Response.Write(sb.ToString())
        'Response.End()

        Dim gridView_toExport As New GridView
        IControles.fillGridViewPaging(gridView_toExport, Session("RepMovi"))

        For i As Integer = 0 To gridView_toExport.Rows.Count - 1
            Dim row As GridViewRow = gridView_toExport.Rows(i)
        Next
        
        Dim sb As StringBuilder = New StringBuilder()
        Dim sw As IO.StringWriter = New IO.StringWriter(sb)
        Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
        Dim pagina As Page = New Page
        Dim form = New HtmlForm
        pagina.EnableEventValidation = False
        pagina.DesignerInitialize()
        pagina.Controls.Add(form)
        gridView_toExport.AllowPaging = False
        form.Controls.Add(gridView_toExport)
        pagina.RenderControl(htw)
        Response.Clear()
        Response.Buffer = True
        Response.ContentType = "application/vnd.ms-excel"

        Response.AddHeader("Content-Disposition", "attachment;filename=movT" & Now.ToString("_dd_MMMM_yy_HH_mm_ss") & ".xls")
        Response.Charset = "UTF-8"
        Response.ContentEncoding = Encoding.Default
        Response.Write(sb.ToString())
        Response.End()

    End Sub

    Protected Sub btn_excel_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btn_excel.Click
        'exportToExcel(gvDetalleCartera)
        exportToExcel(Session("RepMovi"))
    End Sub

    

    Protected Sub gvDetalleCartera_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvDetalleCartera.PageIndexChanging
        IControles.fillGridViewPaging(gvDetalleCartera, hf_repDC.Value, Convert.ToInt32(e.NewPageIndex))
    End Sub

    
End Class
